package codeRandomThoughts.Test1615最大网络秩;

/**
 * 2023-03-15 16:44
 */
public class Solution {
    public int maximalNetworkRank(int n, int[][] roads) {
        //准备工作
        boolean[][] connect = new boolean[n][n];//记录城市间是否相连
        int[] degree = new int[n];//记录每个城市的度数,即与几条路相连

        for (int[] road : roads) {
            //道路两端的城市
            int a = road[0];
            int b = road[1];

            //相连矩阵初始化
            connect[a][b] = true;
            connect[b][a] = true;

            //度数初始化
            degree[a]++;
            degree[b]++;
        }

        int maxRank = 0;
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {
                //i与j无通路:rank = degree[i] + degree[j]
                //i与j有通路:rank = degree[i] + degree[j] - 1
                int rank = degree[i] + degree[j];
                if (connect[i][j]) rank--;
                maxRank = Math.max(rank, maxRank);
            }
        }

        return maxRank;
    }
}
